<?php
/**
 * 需求调拨申请系统自动审核规则模型
 * 自动审核规则主要用于系统是否可以自动审核调拨需求判断
 */
class Cron_Model_Backup_TransferApplyAutoCheckRule extends Model_Base {

	public function __construct() {
		$this->db = $this->db();
	}

	public function getId($transferFromWarehouse, $transferToWarehouse) {
		$where = "";
		$where .= $this->db->quoteInto("from_warehouse=?", $transferFromWarehouse);
		$where .= " AND " . $this->db->quoteInto("to_warehouse=?", $transferToWarehouse);
		return $this->db->fetchOne('select id from b_transfer_apply_auto_check_rule  where ' . $where);
	}

	/**
	 * 根据调出仓和调入仓获取系统自动审核规则
	 * @param  [type] $transferFromWarehouse 调出仓
	 * @param  [type] $transferToWarehouse   调入仓
	 * @return [type]                        [description]
	 */
	public function getOneDetail($transferFromWarehouse, $transferToWarehouse) {
		//为什么要放在静态变量？因为每一条调拨申请都需要检查是否符合系统自动审核规则，
		//从而需要循环获取审核规则，为了避免重复查询数据库，所以这样
		$cacheKey = md5($transferFromWarehouse) . md5($transferToWarehouse);
		static $allRule = [];
		if (isset($allRule[$cacheKey])) {
			return $allRule[$cacheKey];
		}

		$where = "";
		$where .= $this->db->quoteInto("rule.from_warehouse=?", $transferFromWarehouse);
		$where .= " AND " . $this->db->quoteInto("rule.to_warehouse=?", $transferToWarehouse);
		$warehouseRule = $this->db->fetchAll('select rule.*,rule_item.* from b_transfer_apply_auto_check_rule as rule left join b_transfer_apply_auto_check_rule_item as rule_item on rule_item.auto_check_rule_id=rule.id where ' . $where . ' AND rule_item.id is not null');
		if ($warehouseRule) {
			$allRule[$cacheKey] = $warehouseRule;
		}

		return $warehouseRule;
	}

	/**
	 * 获取所有的系统自动审核规则
	 * @return [type] [description]
	 */
	public function getAll() {
		return $this->db->fetchAll('select rule_item.*,rule.* from b_transfer_apply_auto_check_rule as rule left join b_transfer_apply_auto_check_rule_item as rule_item on rule_item.auto_check_rule_id=rule.id where rule_item.id is not null');
	}

	/**
	 * 调拨申请是否符合系统自动审核规则
	 * @param  array   $oneTransferApply 一条调拨申请记录
	 * @return bool,发生错误抛出异常，不符合系统自动审核规则返回false,符合返回true
	 */
	public function isConform(array $oneTransferApply) {
		$applyArrNeedKey = ['goods_sn', 'from_warehouse', 'to_warehouse', 'apply_quantity', 'can_transfer_quantity'];
		foreach ($applyArrNeedKey as $needKey) {
			if (!isset($oneTransferApply[$needKey])) {
				throw new Exception("调拨申请是否符合系统自动审核方法参数缺少字段:" . $needKey);
			}
		}

		$goodsSn = $oneTransferApply['goods_sn'];
		$transferFromWarehouse = $oneTransferApply['from_warehouse'];
		$transferToWarehouse = $oneTransferApply['to_warehouse'];

		$autoCheckRule = $this->getOneDetail($transferFromWarehouse, $transferToWarehouse);
		if (empty($autoCheckRule)) {
			throw new Exception("未找到仓库" . $transferFromWarehouse . '调拨到仓库' . $transferToWarehouse . "自动审核规则！");
		}

		$allRuleCheckMethod = ['_lifeCycleCheck'];
		$sysNeedCheckQuantity = min($oneTransferApply['apply_quantity'], $oneTransferApply['can_transfer_quantity']);

		$isConform = true;
		foreach ($allRuleCheckMethod as $ruleCheckMethod) {
			$isConform = $this->$ruleCheckMethod($autoCheckRule, $sysNeedCheckQuantity, $goodsSn, $transferFromWarehouse);
			if ($isConform === false) {
				return false;
			}
		}

		return $isConform;
	}

	/**
	 * 系统自动审核调拨申请-产品生命周期限制检查
	 * @param  [type] $checkRules        [description]
	 * @param  [type] $needCheckQuantity [description]
	 * @param  [type] $goodsSn           [description]
	 * @return [type]                    [description]
	 */
	private function _lifeCycleCheck($checkRules, $needCheckQuantity, $goodsSn, $warehouse) {
		$productCurrLifeCycle = $this->db->fetchOne('select life_cycle from p_product_warehouse_stock where goods_sn=' . $this->db->quote($goodsSn) . " AND warehouse_code=" . $this->db->quote($warehouse));

		foreach ($checkRules as $rule) {
			if ($rule['condition_type'] == 'lifeCycle' && $rule['condition_eq_value'] == $productCurrLifeCycle) {
				if ($needCheckQuantity >= $rule['check_quantity_lower'] && $needCheckQuantity <= $rule['check_quantity_upper']) {
					return true;
				}
			}
		}
		return false;
	}

}